package com.booking.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.booking.bean.FileImage;
import com.booking.bean.Food;
import com.booking.bean.PageBean;
import com.booking.bean.Seller;
import com.booking.service.FileUpload;
import com.booking.service.FoodQueryService;
import com.booking.service.SellerQueryService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class FoodMrgAction extends ActionSupport  implements ModelDriven<Food>{
	
	//模型驱动使用的对象
	private Food food = new Food();
	@Override
	public Food getModel() {
		return food;
	}
	
	@Autowired
	private FoodQueryService foodQueryService;
	@Autowired
	private FileUpload fileUpload;
	
	protected FileImage fileImage;
	public FileImage getFileImage() {
		return fileImage;
	}
	public void setFileImage(FileImage fileImage) {
		this.fileImage = fileImage;
	}
	private String ids;
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	
	public void save() {
		String pic = fileUpload.uploadFile(fileImage);
		food.setPic(pic);
		foodQueryService.saveFood(food);
	}
	
	
	public void update() {
		String pic = null;
		if(fileImage != null)
			pic = fileUpload.uploadFile(fileImage);
		food.setPic(pic);
		foodQueryService.updateFood(food);
	}
	
	
	public String  queryFood() throws IOException{

		List<Food> list = foodQueryService.queryFood(food.getSellerid());
	
		  JSONArray object = JSONArray.fromObject(list);
          HttpServletResponse response = ServletActionContext.getResponse();
          response.setContentType("html/text;charset=UTF-8");
          response.getWriter().write(object.toString());  
          return null;  
	}
	public void deleteByIds() {
		foodQueryService.deleteByIds(ids);
	}

}
